package com.example.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest implements Runnable{
    private Integer ticketNum=100;
    Lock lock=new ReentrantLock();
    @Override
    public void run() {
        while (true){
            lock.lock();

            try {
                if(ticketNum>0) {
                    System.out.println(Thread.currentThread().getName() + "票" + ticketNum--);
                } else {
                    System.out.println("票已卖完");
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
            }
    }

    public static void main(String[] args) {
        SynchorizedTest synchorizedTest = new SynchorizedTest();
        new Thread(synchorizedTest,"A").start();
        new Thread(synchorizedTest,"B").start();
        new Thread(synchorizedTest,"C").start();
        new Thread(synchorizedTest,"D").start();
        new Thread(synchorizedTest,"E").start();
        new Thread(synchorizedTest,"F").start();
    }
}
